package com.devplatform.service.impl.article;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.devplatform.mapper.article.ArticleInteractionsMapper;
import com.devplatform.mapper.article.ArticleShareMapper;
import com.devplatform.pojo.article.ArticleShare;
import com.devplatform.pojo.vo.DataVo;
import com.devplatform.service.article.ArticleService;
import com.devplatform.service.article.ArticleShareService;
import com.devplatform.service.points.PointsCommonService;
import com.devplatform.service.points.UserPointsService;
import com.devplatform.util.UserContextHolder;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Jnks03
 * @description 针对表【article_share】的数据库操作Service实现
 * @createDate 2025-09-29 11:41:35
 */
@Service
@RequiredArgsConstructor
public class ArticleShareServiceImpl extends ServiceImpl<ArticleShareMapper, ArticleShare>
        implements ArticleShareService {
    //每次分享加的积分数
    private static final Integer ARTICLE_POINTS = 5;

    private final ArticleInteractionsMapper interactionsMapper;
    private final UserPointsService userPointsService;
    private final ArticleService articleService;
    private final PointsCommonService pointsCommonService;

    //生成对应文章的URL地址
    @Override
    public String generateShareUrl(Long articleId) {
        Long authorId = articleService.getAuthorIdByArticleId(articleId);
        return "http://localhost:5173/article/" + articleId;
    }

    //分享文章加积分
    @Override
    //标注事务管理 回滚
    @Transactional(rollbackFor = Exception.class)
    public DataVo confirmShare(Long articleId) {
        Long shareUserId = UserContextHolder.getUserId();
        Long authorId = articleService.getAuthorIdByArticleId(articleId);
        pointsCommonService.updatePointsDetail(
                shareUserId,
                "ARTICLE_POINTS",
                ARTICLE_POINTS,
                true // 发送通用积分消息
        );
        interactionsMapper.incrementShare(articleId);
        return DataVo.ok();
    }
}




